<cfcomponent>

	<cffunction name="Init" access="public" output="no" hint="Initialises the component">
    	<cfargument name="configFilePath" type="string" required="no" default="#GetDirectoryFromPath(GetCurrentTemplatePath())#feedconfig.xml.cfm" hint="The full filename/path to the config file for the component, defaults to feedconfig.xml.cfm in the same folder as the CFC">
    	<cfset VARIABLES.configFilePath = ARGUMENTS.configFilePath>
		<!--- this will store the feed info of each feed --->
		<cfset VARIABLES.feedQueryArray = ArrayNew("1")>
		<!--- this will store the current feed info --->
		<cfset VARIABLES.allFeedsQuery = QueryNew("nothing")>
        <cfset VARIABLES.LockName="lck_" & CreateUUID()>
        <cfreturn THIS>
    </cffunction> 
    
    <cffunction name="GetFilePath" output="no" access="public" returntype="string">
    	<cfreturn VARIABLES.configFilePath>
    </cffunction>
    
    <cffunction name="GetAllFeedsQuery" output="no" access="public" returntype="query" hint="Returns the all feeds query">
    	<cfreturn VARIABLES.allFeedsQuery>
    </cffunction>
    
    <cffunction name="GetFeedQueryArray" output="no" access="public" returntype="array" hint="Returns the feeds array">
    	<cfreturn VARIABLES.feedQueryArray>
    </cffunction>

    <cffunction name="DoPing" output="no" access="public" returntype="boolean" hint="Pings a given feed and adds its query to the feed data">
    	<cfargument name="FeedID" required="yes" type="numeric" hint="The ID of the feed to add">
        <cfset var configXML = XMLParse(VARIABLES.configFilePath)>
        <cfset var thisFeed = XmlSearch(configXML,"//feed[@id='#ARGUMENTS.FeedID#']")>
        <cfset var feedURL = thisFeed[1].url.XMLText>
        <cfset var feedName = thisFeed[1].blogname.XMLText>
        <cfset var feedquery = GetFeed(feedURL,feedName)>
        <cflock type="exclusive" name="#VARIABLES.LockName#" timeout="120">
			<cfset VARIABLES.feedQueryArray[ARGUMENTS.FeedID] = feedquery>
            <cfset CombineFeeds()>
        </cflock>
        <cfreturn TRUE>
    </cffunction>
    
    <cffunction name="DoAllPings" output="no" access="public" returntype="boolean" hint="Pings all feeds and adds the queries to the feed data">
        <cfset var feedURL = "">
        <cfset var feedName = "">
        <cfset var feedquery = "">
		<cfset var configXML = XMLParse(VARIABLES.configFilePath)>
        <cfset var allFeeds = XmlSearch(configXML,"//feed")>
        <cfset var i = 1>
        <cflock type="exclusive" name="#VARIABLES.LockName#" timeout="120">
            <cfloop condition="i LTE ArrayLen(allFeeds)">
                <cfset feedURL = allFeeds[i].url.XMLText>
                <cfset feedName = allFeeds[i].blogname.XMLText>
                <cfset feedquery = GetFeed(feedURL,feedName)>
                <cfset VARIABLES.feedQueryArray[i] = feedquery>
                <cfset i = i + 1>
            </cfloop>
            <cfset CombineFeeds()>
        </cflock>
        <cfreturn TRUE>
    </cffunction>
    
    <cffunction name="GetFeed" output="no" access="private" returntype="query" hint="Fetches a feed, parses it and returns it as a query">
		<cfargument name="feedURL" type="string" required="yes" hint="The URL of the feed to fetch">
        <cfargument name="feedName" type="string" required="yes" hint="The name to give this feed in the returned query">
        <cfset var qXML = "">
        <cfset var blogNameArray = ArrayNew(1)>
				<cfset var pubdateArray = ArrayNew(1)>
				<cffeed source="#ARGUMENTS.feedURL#" query="qXML"> 
        <cfset ArraySet(blogNameArray,1,qXML.RecordCount,ARGUMENTS.feedName)>
				<cfloop query="qXML">
					<cfset pubdateArray[qXML.currentrow] = parseDateTime(qXML.publishedDate)>
				</cfloop>
				<cfset ArraySet(blogNameArray,1,qXML.RecordCount,ARGUMENTS.feedName)>
        <cfset QueryAddColumn(qXML,"blogname","VARCHAR",blogNameArray)>
				<cfset QueryAddColumn(qXML,"pubdateobject","DATE",pubdateArray)>
        <cfreturn qXML>
    </cffunction>
    
    <cffunction name="CombineFeeds" output="no" access="private" returntype="boolean" hint="Combines all the stored feed queries into an aggregated feed">
    	<cfset var QFeeds  = "">
        <cfset var i = 1>
        <cfset var thisQuery = StructNew()>
        <cfquery dbtype="query" name="QFeeds">
        	(<cfloop condition="i LTE ArrayLen(VARIABLES.feedQueryArray)">
            	<cfset thisquery["num_" & i] = VARIABLES.feedQueryArray[i]>
                SELECT TITLE,PUBLISHEDDATE,CONTENT,RSSLINK,BLOGNAME,pubdateobject
                  FROM thisQuery.num_#i#
                <cfif i NEQ ArrayLen(VARIABLES.feedQueryArray)>
                 UNION
				</cfif>
                <cfset i = i + 1>
            </cfloop>)
            ORDER BY pubdateobject DESC
        </cfquery>
        <cfset VARIABLES.allFeedsQuery = QFeeds>
        <cfreturn TRUE>
    </cffunction>


</cfcomponent>
